package com.darwin.darwinUI.controls
{
	import com.darwin.darwinUI.assist.Group;
	import com.darwin.darwinUI.assist.IGroupItem;
	import com.darwin.darwinUI.assist.IGroupTarget;
	import com.darwin.darwinUI.core.UISprite;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;
	
	/**
	 * 简易选项卡面板切换器，它的作用是将特定选项卡和面板关联起来，并在选项卡点击时自动实现切换，TGroupTarget的一种简单实现
	 * @author Roland
	 */
	public class TabViewStack implements IGroupTarget
	{
		protected var _panelDic:Dictionary;//面板字典
		protected var _group:Group;
		protected var _container:UISprite;
		protected var _currentPanel:ITabPanel;
		protected var _currentTab:IGroupItem;
		/**
		 *	实例化一个选项卡面板切换组件 
		 * @param container 用来放置选项卡显示面板的容器
		 * 
		 */
		public function TabViewStack(container:UISprite)
		{
			_panelDic = new Dictionary();
			_group = new Group();
			_group.registerTarget(this);
			_container = container;
		}
		
		/**
		 *	将面板与某tab进行绑定
		 * @param panel 面板(需实现ITabPanel接口)
		 * @param tab 要与面板绑定的tab
		 */
		public function addBinding(panel:ITabPanel,tab:IGroupItem):void
		{
			_panelDic[tab] = panel;
			_group.registerItem(tab);
		}
		
		/**
		 * 根据给定tab移除与某个面板绑定
		 * @param tab
		 * 
		 */
		public function removeBindingByTab(tab:IGroupItem):void
		{
			delete _panelDic[tab];
			_group.unregisterItem(tab);
		}
		
		/**
		 *	根据给定面板移除与tab的绑定 
		 * @param panel
		 * 
		 */
		public function removeBindingByPanel(panel:ITabPanel):void
		{
			for(var tab:Object in _panelDic) 
			{
				if(_panelDic[tab] === panel)
				{
					delete _panelDic[tab];
					if(tab is IGroupItem)
					{
						_group.unregisterItem(tab as IGroupItem);
					}
					break;
				}
			}
		}
		
		/**
		 *	面板切换动画，覆盖此方法来实现自定义的切换效果 
		 * @param tab 要切换到的面板所绑定的tab
		 * 
		 */
		protected function panelSwitchAnimation(tab:IGroupItem):void
		{
			if(_container)
			{
				_container.removeAllChild();
				
				_currentPanel = _panelDic[tab];
				_currentTab = tab;
				if(_currentPanel is DisplayObject)
				{
					_container.addChild(_currentPanel as DisplayObject);
				}
				_currentPanel.tabSelectionChange(_currentTab);
			}
		}
		
		public function itemSelectionChange(item:IGroupItem, selected:Boolean):void
		{
			panelSwitchAnimation(item);
		}
	}
}